package com.example.weibo3.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.weibo3.R;
import com.example.weibo3.bean.MessageItem;
import com.example.weibo3.bean.WeiboItem;

import java.util.List;

public class WeiboAdapter extends ArrayAdapter<WeiboItem> {
    public WeiboAdapter(@NonNull Context context, int resource, @NonNull List<WeiboItem> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    // 返回每一条微博
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 加载布局文件，生成一个view
        View view = LayoutInflater.from(getContext()).inflate(R.layout.weibo_list_item, parent, false);

        // 获取数据，通过position获取数组中对应的一项数据
        WeiboItem messageItem = getItem(position);

        // 什么数据渲染到什么组件上
        // 头像图片渲染填充
        ImageView imageView = view.findViewById(R.id.imageView);
        Glide.with(getContext())
                .load(messageItem.getAvatar())
                .into(imageView);

        // 用户名
        TextView textView = view.findViewById(R.id.webo_card_name);
        textView.setText(messageItem.getName());
        // 微博内容
        TextView contentView = view.findViewById(R.id.content);
        contentView.setText(messageItem.getContent());

        TextView shareView = view.findViewById(R.id.share);
        shareView.setText(messageItem.getShares()+"");

        TextView commentView = view.findViewById(R.id.comment);
        commentView.setText(messageItem.getComments()+"");

        final TextView likeView = view.findViewById(R.id.like);
        likeView.setText(messageItem.getLikes()+"");

        TextView timeView = view.findViewById(R.id.time);
        timeView.setText(messageItem.getDate());

        // 取图片数据，每一条微博的所有图片数据
        List<String> images = messageItem.getImages();
        GridViewAdapter gridViewAdapter =
                new GridViewAdapter(getContext(), R.layout.gridview_item, images);

        GridView gridView = view.findViewById(R.id.image_grid_view);
        gridView.setAdapter(gridViewAdapter);
        return view;
    }
}
